#version 300 es

precision highp float;

flat in vec4 pointValue;
in float pointX;

out vec4 fragColor;

uniform int algorithm;

float interpolation_linear() {
    float xDiff = pointValue.x - pointValue.z;
    float yDiff = pointValue.y - pointValue.w;
    float y = (pointX - pointValue.z) * yDiff / xDiff + pointValue.w;
    return y;
}

float interpolation_nearest() {
    float mid = (pointValue.x + pointValue.z) / 2.0;
    return pointX <= mid ? pointValue.w : pointValue.y;
}

void main() {
    float y;
    if (algorithm == 0)
        y = interpolation_linear();
    else if (algorithm == 1)
        y = interpolation_nearest();

    fragColor = vec4(pointX, y, 0, 1);
}
